package com.societyGames.flashForms
{
import flash.events.Event;

/*
Scales as large as possible while maintaining aspect ratio.
 */
public class ScaleContainer extends SingleItemContainer
{
  private var _desiredWidth:Number;
  private var _desiredHeight:Number;

  public override function set width(value:Number):void
  {
    this._desiredWidth = value;
    recalculate();
  }

  public override function set height(value:Number):void
  {
    this._desiredHeight = value;
    recalculate();
  }

  protected override function childSizeChangedHandler(event:Event):void
  {
    recalculate();
  }

  private function recalculate():void
  {
    if (this._item != null)
    {
      this._item.scaleX = 1;
      this._item.scaleY = 1;
      var scale:Number = Math.min(
              this._desiredWidth / this._item.width,
              this._desiredHeight / this._item.height
      );
      this._item.scaleX = scale;
      this._item.scaleY = scale;
      this._item.x = (this._desiredWidth - this._item.width) / 2;
      this._item.y = (this._desiredHeight - this._item.height) / 2;
    }
  }
}
}